package metalink;

import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.sql.*;

import org.apache.struts2.ServletActionContext;


import vo.*;
import vo.mixed.*;
import model.admin.*;
import myutil.*;

import java.io.*;

import javax.servlet.http.HttpSession;

public class GroupMainDetails extends ActionSupport {
	BeanGroupDetails bean_group_details;
	int user_id, group_id, group_update_id, c_cool_count, c_agree_count;
	String group_name, last_update_date, create_date, owner_id;
	ArrayList<BeanGroupDetails> groups_by_owner;
	ArrayList<BeanGroupDetails> groups;
	ArrayList<BeanGroupMember> group_members;
	ArrayList<BeanGroupUpdates> group_updates;
	ArrayList<BeanGroupUpdatesComplete> all_updates;
	BeanGroupUpdates header;
	HttpSession sessioncheck;
	HttpSession session;
	
	public String execute(){
		sessioncheck = ServletActionContext.getRequest().getSession();
		try{
			groups_by_owner = new ArrayList<BeanGroupDetails>();
			groups = new ArrayList<BeanGroupDetails>();
			String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
			getUserId(username);
			groups = new ArrayList<BeanGroupDetails>();
			Connect c=new Connect();
			ResultSet rs;
			group_members = new ArrayList<BeanGroupMember>();
			rs = c.getResult("select * From groups g where group_id='"+group_id+"'");
			while(rs.next()){
				ResultSet rs1 = c.getResult("select * from student_details where student_id in (select distinct(student_member_id) from groups_member where group_id='"+group_id+"')");
				while(rs1.next()){
					BeanGroupMember bgr = new BeanGroupMember(rs1.getString("student_name"), rs1.getString("email_id"));
					group_members.add(bgr);
				}
				rs1 = c.getResult("select * from college_staff where staff_id in (select distinct(staff_member_id) from groups_member where group_id='"+group_id+"')");
				while(rs1.next()){
					BeanGroupMember bgr = new BeanGroupMember(rs1.getString("staff_name"), rs1.getString("email_id"));
					group_members.add(bgr);
				}
				int member_count = group_members.size();
				String owner_name = null;
				//bean_group_details = new BeanGroupDetails(group_id, rs.getInt("owner_id"), member_count, rs.getString("group_name"), rs.getString("create_date"), rs.getString("last_update_date"), group_members);
				//groups.add(bgd);
				int owner_id = rs.getInt("owner_id");
				rs1 = c.getResult("select student_name from student_details where student_id='"+owner_id+"'");
				while(rs1.next()){
					owner_name = rs1.getString("student_name");
				}
				bean_group_details = new BeanGroupDetails(group_id, rs.getInt("owner_id"), owner_name, member_count, rs.getString("group_name"), rs.getString("create_date"), rs.getString("last_update_date"), group_members);
				group_name = bean_group_details.getGroup_name();
				create_date = bean_group_details.getCreate_date();
				last_update_date = bean_group_details.getLast_update_date();
			}
			
			all_updates = new ArrayList<BeanGroupUpdatesComplete>();
			rs = c.getResult("select * from groups_update where group_id='"+group_id+"' and parent_id='0' order by update_time desc");
			while(rs.next()){
				group_updates = new ArrayList<BeanGroupUpdates>();
				int updater_id = rs.getInt("updater_id");
				String updater_role = rs.getString("updater_role");
				if(updater_role.equals("student")){
					ResultSet rs2 = c.getResult("select student_name from student_details where student_id='"+updater_id+"'");
					while (rs2.next()){
						header = new BeanGroupUpdates(rs.getInt("group_update_id"), rs.getInt("group_id"), updater_id, rs.getInt("parent_id"), rs.getInt("document_upload_id"), rs.getInt("status"), rs.getInt("cool_count"), rs.getInt("agree_count"), rs.getString("update"), updater_role, rs.getString("parent_role"), rs.getString("update_time"), rs2.getString("student_name") );
					}
				}
				else {
					ResultSet rs2 = c.getResult("select staff_name from college_staff where staff_id='"+updater_id+"'");
					while(rs2.next()){
						header = new BeanGroupUpdates(rs.getInt("group_update_id"), rs.getInt("group_id"), updater_id, rs.getInt("parent_id"), rs.getInt("document_upload_id"), rs.getInt("status"), rs.getInt("cool_count"), rs.getInt("agree_count"), rs.getString("update"), updater_role, rs.getString("parent_role"), rs.getString("update_time"), rs2.getString("staff_name") );
					}
				}
				System.out.println(header.getGroup_update_id());
				ResultSet rs1 = c.getResult("select * from groups_update where group_id='"+group_id+"' and parent_id='"+header.getGroup_update_id()+"' order by update_time desc");
				while(rs1.next()){
					int c_updater_id = rs1.getInt("updater_id");
					String c_updater_role = rs1.getString("updater_role");
					c_cool_count = rs1.getInt("cool_count");
					c_agree_count = rs1.getInt("agree_count");
					session = ServletActionContext.getRequest().getSession();
					session.setAttribute("c_cool_count", c_cool_count);
					session.setAttribute("c_agree_count", c_agree_count);
					System.out.println(c_cool_count);
					System.out.println(c_agree_count);
					if(c_updater_role.equals("student")){
						ResultSet rs2 = c.getResult("select student_name from student_details where student_id='"+c_updater_id+"'");
						while (rs2.next()){
							BeanGroupUpdates bgu = new BeanGroupUpdates(rs1.getInt("group_update_id"), rs1.getInt("group_id"), c_updater_id, rs1.getInt("parent_id"), rs1.getInt("document_upload_id"), rs1.getInt("status"), c_cool_count, c_agree_count, rs1.getString("update"), c_updater_role, rs1.getString("parent_role"), rs1.getString("update_time"), rs2.getString("student_name") );
							group_updates.add(bgu);
						}
					}
					else {
						ResultSet rs2 = c.getResult("select staff_name from college_staff where staff_id='"+c_updater_id+"'");
						while(rs2.next()){
							BeanGroupUpdates bgu = new BeanGroupUpdates(rs1.getInt("group_update_id"), rs1.getInt("group_id"), c_updater_id, rs1.getInt("parent_id"), rs1.getInt("document_upload_id"), rs1.getInt("status"), rs1.getInt("cool_count"), rs1.getInt("agree_count"), rs1.getString("update"), c_updater_role, rs1.getString("parent_role"), rs1.getString("update_time"), rs2.getString("staff_name") );
							group_updates.add(bgu);
						}
					}
				}
				BeanGroupUpdatesComplete bguc = new BeanGroupUpdatesComplete(header.getGroup_update_id(), header.getGroup_id(), header.getUpdater_id(), header.getParent_id(), header.getDocument_upload_id(), header.getStatus(), header.getUpdate(), header.getUpdater_role(), header.getParent_role(), header.getUpdate_time(), header.getUpdater_name(), group_updates, group_updates.size(), header.getCool_count(), header.getAgree_count());
				all_updates.add(bguc);
			}
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 	ERROR;
		}
	}
	
	public void getUserId(String username){
		Connect c=new Connect();
		ResultSet rs;
		try {
			if(sessioncheck.getAttribute("user_type").equals("admin") || sessioncheck.getAttribute("user_type").equals("professor"))
				{
				rs = c.getResult("select staff_id from college_staff where email_id='"+username+"'");
				while(rs.next()){
					user_id = rs.getInt("staff_id");
				}
			}
			else if(sessioncheck.getAttribute("user_type").equals("student"))
			{
			rs = c.getResult("select student_id from student_details where email_id='"+username+"'");
			while(rs.next()){
				user_id = rs.getInt("student_id");
			}
		}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public BeanGroupDetails getBean_group_details() {
		return bean_group_details;
	}
	public void setBean_group_details(BeanGroupDetails bean_group_details) {
		this.bean_group_details = bean_group_details;
	}
	public int getUser_id() {
		return user_id;
	}
	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}
	public int getGroup_id() {
		return group_id;
	}
	public void setGroup_id(int group_id) {
		this.group_id = group_id;
	}
	public String getGroup_name() {
		return group_name;
	}
	public void setGroup_name(String group_name) {
		this.group_name = group_name;
	}
	public String getLast_update_date() {
		return last_update_date;
	}
	public void setLast_update_date(String last_update_date) {
		this.last_update_date = last_update_date;
	}
	public String getCreate_date() {
		return create_date;
	}
	public void setCreate_date(String create_date) {
		this.create_date = create_date;
	}
	public String getOwner_id() {
		return owner_id;
	}
	public void setOwner_id(String owner_id) {
		this.owner_id = owner_id;
	}
	public ArrayList<BeanGroupDetails> getGroups_by_owner() {
		return groups_by_owner;
	}
	public void setGroups_by_owner(ArrayList<BeanGroupDetails> groups_by_owner) {
		this.groups_by_owner = groups_by_owner;
	}
	public ArrayList<BeanGroupDetails> getGroups() {
		return groups;
	}
	public void setGroups(ArrayList<BeanGroupDetails> groups) {
		this.groups = groups;
	}
	public ArrayList<BeanGroupMember> getGroup_members() {
		return group_members;
	}
	public void setGroup_members(ArrayList<BeanGroupMember> group_members) {
		this.group_members = group_members;
	}
	public ArrayList<BeanGroupUpdates> getGroup_updates() {
		return group_updates;
	}
	public void setGroup_updates(ArrayList<BeanGroupUpdates> group_updates) {
		this.group_updates = group_updates;
	}
	public ArrayList<BeanGroupUpdatesComplete> getAll_updates() {
		return all_updates;
	}
	public void setAll_updates(ArrayList<BeanGroupUpdatesComplete> all_updates) {
		this.all_updates = all_updates;
	}
	public BeanGroupUpdates getHeader() {
		return header;
	}
	public void setHeader(BeanGroupUpdates header) {
		this.header = header;
	}
	public HttpSession getSessioncheck() {
		return sessioncheck;
	}
	public void setSessioncheck(HttpSession sessioncheck) {
		this.sessioncheck = sessioncheck;
	}

	public int getGroup_update_id() {
		return group_update_id;
	}

	public void setGroup_update_id(int group_update_id) {
		this.group_update_id = group_update_id;
	}

	public int getC_cool_count() {
		return c_cool_count;
	}

	public void setC_cool_count(int c_cool_count) {
		this.c_cool_count = c_cool_count;
	}

	public int getC_agree_count() {
		return c_agree_count;
	}

	public void setC_agree_count(int c_agree_count) {
		this.c_agree_count = c_agree_count;
	}
}
